package com.newdjk.okhttp;

import android.content.Context;

import com.facebook.stetho.okhttp3.StethoInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/*
 *  @项目名：  RetrofitRxjavaDemo
 *  @包名：    com.example.huhai.retrofitrxjavademo.net
 *  @文件名:   RetrofitFactory
 *  @创建者:   EDZ
 *  @创建时间:  2018/11/1 10:48
 *  @描述：
 */
public class RetrofitFactory {
    private Retrofit retrofit;

    private static RetrofitFactory sRetrofitFactory = new RetrofitFactory();

    public static String getToken() {
        return token;
    }

    public static void setToken(String token) {
        RetrofitFactory.token = token;
    }

    private static String token = "";

    private RetrofitFactory() {
//        OkHttpClient mOkHttpClient=new OkHttpClient.Builder()
//                .connectTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
//                .readTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
//                .writeTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
//                .addInterceptor(InterceptorUtil.HeaderInterceptor(RetrofitFactory.token))
//                .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
//                .addNetworkInterceptor(new StethoInterceptor()) //添加抓包工具
//                .build();
//        retrofit = new Retrofit.Builder()
//                .baseUrl(HttpConfig.BASE_URL)
//                .addConverterFactory(MyGsonConverterFactory.create())//添加gson转换器
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
//                .client(mOkHttpClient)
//                .build();
    }

    public void init(Context context, String url, String token) {
        RetrofitFactory.token = token;
        OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
                .readTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
                .writeTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
                .addInterceptor(InterceptorUtil.HeaderInterceptor(RetrofitFactory.token))
                .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
                .addNetworkInterceptor(new StethoInterceptor()) //添加抓包工具
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(MyGsonConverterFactory.create())//添加gson转换器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
                .client(mOkHttpClient)
                .build();
        return;
    }

    public <T> T create(final Class<T> service) {
        return retrofit.create(service);
    }


    public static RetrofitFactory getRetrofitFactory() {
        return sRetrofitFactory;
    }

    public static RetrofitFactory getRetrofitFactory(String token) {
        return sRetrofitFactory;
    }

}
